package cn.ylz.ylmall.content.service.impl;

import cn.ylz.ylmall.account.pojo.vo.PageData;
import cn.ylz.ylmall.account.security.CurrentPrincipal;
import cn.ylz.ylmall.common.ex.ServiceException;
import cn.ylz.ylmall.common.web.ServiceCode;
import cn.ylz.ylmall.content.dao.persist.repository.ICommentRepository;
import cn.ylz.ylmall.content.pojo.entity.Comment;
import cn.ylz.ylmall.content.pojo.param.CommentAddNewParam;
import cn.ylz.ylmall.content.pojo.vo.CommentListItemVO;
import cn.ylz.ylmall.content.pojo.vo.CommentStandardVO;
import cn.ylz.ylmall.content.pojo.vo.ContentStandardVO;
import cn.ylz.ylmall.content.pojo.vo.CountVO;
import cn.ylz.ylmall.content.service.ICommentService;
import cn.ylz.ylmall.product.pojo.vo.PCommentVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Currency;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CommentServiceImpl implements ICommentService {
    @Value("5")
    private Integer defaultQueryPageSize;
    @Autowired
    private ICommentRepository commentRepository;

    /**
     * 发表评论
     * @param commentAddNewParam
     */
    @Override
    public void addNew(CommentAddNewParam commentAddNewParam, CurrentPrincipal currentPrincipal) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddNewParam,comment);
        comment.setUserId(currentPrincipal.getId());
        comment.setCreateTime(new Date());
        System.out.println("检测插入数据是否正确"+comment);
        int rows = commentRepository.insert(comment);
        if(rows !=1){
            String message = "发布失败,请重新发布";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    /**
     * 发表子级评论
     * @param commentAddNewParam
     * @param currentPrincipal
     * @param id
     */
    @Override
    public void addNew(CommentAddNewParam commentAddNewParam, CurrentPrincipal currentPrincipal, Long id) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddNewParam,comment);
        comment.setUserId(currentPrincipal.getId());
        comment.setPid(id);
        comment.setCreateTime(new Date());
        System.out.println("检测插入数据是否正确"+comment);
        int rows = commentRepository.insert(comment);
        if(rows !=1){
            String message = "发布失败,请重新发布";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public List<CommentStandardVO> getChild(Long id) {
        log.debug("开始处理【根据评论id查询子级评论】的请求，参数：{}", id);
        if(commentRepository.getChild(id) == null){
            String message = "查询失败,此评论无回复评论";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
        return commentRepository.getChild(id);
    }

    /**
     * 根据评论id删除评论
     * @param id
     */
    @Override
    public void delete(Long id,CurrentPrincipal currentPrincipal) {
        log.debug("开始处理【根据ID删除评论】的请求，参数：{}", id);
        CommentStandardVO queryResult = commentRepository.getStandardById(id);
        if(queryResult == null){
            String message = "删除失败,尝试删除的内容不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
        if(currentPrincipal.getId().equals(queryResult.getUserId())) {
            int rows = commentRepository.deleteById(id);
            if(rows !=1){
                String message = "删除失败";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_DELETE, message);
            }
        }
        else {
            String message = "删除失败,只能删除自己的评论";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    /**
     * id查询
     * @param id
     * @return
     */
    @Override
    public CommentStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询文章】的请求，参数：{}", id);
        CommentStandardVO queryResult = commentRepository.getStandardById(id);
        if(queryResult == null){
            String message = "查询失败,尝试查询的内容不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
        return queryResult;
    }

    @Override
    public List<CommentListItemVO> listByContentId(Long contentId) {
        log.debug("开始处理【根据文章ID查询评论列表】的请求，参数：{}", contentId);
        List<CommentListItemVO> list = commentRepository.listByContentId(contentId);

        list.forEach(this::setChildren);
        return list;
    }

    @Override
    public CountVO countByContentId(Long contentId) {
        return commentRepository.countByContentId(contentId);
    }


    public void setChildren(CommentListItemVO commentListItemVO) {
        List<CommentListItemVO> children = commentRepository.listByParentId(commentListItemVO.getId());
        if (!children.isEmpty()) {
            commentListItemVO.setChildren(children);
            children.forEach(this::setChildren);
        }
    }
}
